﻿Imports System.IO
Imports MessageBoxEx

Module modBackup
    Public Function Backup_Folder(ByVal folderFrom As String, ByVal folderTo As String,
                                  Optional ByVal boolMove As Boolean = True,
                                  Optional ByVal boolDemonbuddy As Boolean = False) As Boolean

        Dim tTime As DateTime = DateTime.Now
        Dim tFormat As String = "DBBackup-" & tTime.ToString("MMddyy-Hmmss")

        If boolDemonbuddy = True Then

            If Process_Find("Demonbuddy") = True Then

                Dim _Msg =
                        New MsgBoxEx(
                            "Looks like Demonbuddy is currently running, please close Demonbuddy and try again.",
                            frmMain.Text, , MsgBoxExImage.Exclamation)
                _Msg.SetButtons(New String() {"OK"}, New DialogResult() {DialogResult.Yes}, 1)
                _Msg.ShowDialog(frmMain)

                Return False

            End If

        End If

        If boolMove = True Then
            Try

                My.Computer.FileSystem.MoveDirectory(folderFrom, folderTo & "\" & tFormat)

            Catch ex As Exception

                'MsgBox(ex)

            End Try

        Else
            Try

                My.Computer.FileSystem.CopyDirectory(folderFrom, folderTo & "\" & tFormat)

            Catch ex As Exception

                'MsgBox(ex)

            End Try

        End If

        Return True
    End Function

    Public Function Restore_Folder(ByVal folderFrom As String, ByVal folderTo As String,
                                   Optional ByVal boolMove As Boolean = True,
                                   Optional ByVal boolDemonbuddy As Boolean = False) As Boolean

        If boolDemonbuddy = True Then

            If Process_Find("Demonbuddy") = True Then

                Dim _Msg =
                        New MsgBoxEx(
                            "It looks like Demonbuddy is currently running. Please close Demonbuddy to continue.",
                            frmMain.Text, , MsgBoxExImage.Information)
                _Msg.SetButtons(New String() {"OK"}, New DialogResult() {DialogResult.Yes}, 1)
                _Msg.ShowDialog(frmMain)

                Return False

            End If

        End If

        If Directory.Exists(folderTo) Then

            Dim _MsgBackup =
                    New MsgBoxEx("The directory you are restoring to already exists. How would you like to proceed?",
                                 frmMain.Text, , MsgBoxExImage.Question)
            _MsgBackup.SetButtons(New String() {"Backup", "Delete", "Cancel"},
                                  New DialogResult() {DialogResult.Yes, DialogResult.No, DialogResult.Cancel}, 1)
            _MsgBackup.ShowDialog(frmMain)

            If _MsgBackup.DialogBoxResult = DialogBoxResult.Button1 Then

                Backup_Folder(folderTo, My.Settings.FolderBackup, True, True)

            ElseIf _MsgBackup.DialogBoxResult = DialogBoxResult.Button2 Then

                Directory.Delete(folderTo)

            Else

                Return Nothing

            End If

        End If

        If boolMove = True Then
            My.Computer.FileSystem.MoveDirectory(folderFrom, folderTo)
        Else
            My.Computer.FileSystem.CopyDirectory(folderFrom, folderTo)
        End If

        Return True
    End Function
End Module